package mr.flow.weblog.bean;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import org.apache.hadoop.io.WritableComparable;
import org.junit.Test;

/**
 * sessionId 
 * @author 汤小萌
 *
 */
public class PageViewsBean implements WritableComparable<PageViewsBean> {
	
	private String session;			// sessionId
	private String remote_addr;		// 客户端ip地址
	private String timeStr;			// 访问的时间
	private String request;			// 请求的url
	private int step;				// 访问的第几步
	private String staylong;		// 停留的时间
	private String referal;			// 是从哪个页面过来的
	private String useragent;		// 记录跟浏览器相关信息
	private String bytes_send;		// 发送的数据字节大小
	private String status;			// 本次请求的状态
	
	public void set(String session, String remote_addr, String useragent, String timeStr, String request, int step, String staylong, String referal, String bytes_send, String status) {
		this.session = session;
		this.remote_addr = remote_addr;
		this.useragent = useragent;
		this.timeStr = timeStr;
		this.request = request;
		this.step = step;
		this.staylong = staylong;
		this.referal = referal;
		this.bytes_send = bytes_send;
		this.status = status;
	}
	
	@Override
	public void write(DataOutput out) throws IOException {
		out.writeUTF(session);
		out.writeUTF(remote_addr);
		out.writeUTF(timeStr);
		out.writeUTF(request);
		out.writeInt(step);
		out.writeUTF(staylong);
		out.writeUTF(referal);
		out.writeUTF(useragent);
		out.writeUTF(bytes_send);
		out.writeUTF(status);
	}
	@Override
	public void readFields(DataInput in) throws IOException {
		this.session = in.readUTF();
		this.remote_addr = in.readUTF();
		this.timeStr = in.readUTF();
		this.request = in.readUTF();
		this.step = in.readInt();
		this.staylong = in.readUTF();
		this.referal = in.readUTF();
		this.useragent = in.readUTF();
		this.bytes_send = in.readUTF();
		this.status = in.readUTF();
	}
	
	@Override
	public int compareTo(PageViewsBean o) {
		// 【注意：】这个session也要先进行比较，只有先进行了session的比较后面的step的比较菜有意义
		int sessionCompareResult = this.session.compareTo(o.getSession());
		if(sessionCompareResult == 0) {  // 相同的session的话就继续比较  step
			return this.step - o.getStep() > 0 ? 1 : -1;   // 这种方式 是正序  从小岛大排序
		} 
		return sessionCompareResult;
		// return 0;
	}

	public String getSession() {
		return session;
	}

	public void setSession(String session) {
		this.session = session;
	}

	public String getRemote_addr() {
		return remote_addr;
	}

	public void setRemote_addr(String remote_addr) {
		this.remote_addr = remote_addr;
	}

	public String getTimeStr() {
		return timeStr;
	}

	public void setTimeStr(String timeStr) {
		this.timeStr = timeStr;
	}

	public String getRequest() {
		return request;
	}

	public void setRequest(String request) {
		this.request = request;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public String getStaylong() {
		return staylong;
	}

	public void setStaylong(String staylong) {
		this.staylong = staylong;
	}

	public String getReferal() {
		return referal;
	}

	public void setReferal(String referal) {
		this.referal = referal;
	}

	public String getUseragent() {
		return useragent;
	}

	public void setUseragent(String useragent) {
		this.useragent = useragent;
	}

	public String getBytes_send() {
		return bytes_send;
	}

	public void setBytes_send(String bytes_send) {
		this.bytes_send = bytes_send;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	
	@Override
	public String toString() {
		return this.session + " " + this.step + "";
	}
	
	
	@Test
	public void testCompareTo() {
		PageViewsBean pvb1 = new PageViewsBean();
		pvb1.set(null, null, null, null, null, 2, null, null, null, null);
		PageViewsBean pvb2 = new PageViewsBean();
		pvb2.set(null, null, null, null, null, 1, null, null, null, null);
		PageViewsBean pvb3 = new PageViewsBean();
		pvb3.set(null, null, null, null, null, 4, null, null, null, null);
		PageViewsBean pvb4 = new PageViewsBean();
		pvb4.set(null, null, null, null, null, 3, null, null, null, null);
		
		ArrayList<PageViewsBean> list = new ArrayList<PageViewsBean>();
		list.add(pvb1);
		list.add(pvb2);
		list.add(pvb3);
		list.add(pvb4);
		
		
		System.out.println(list);
		Collections.sort(list);
		System.out.println(list);
		
	}
	
	
	
	
	
}
